package com.msmall.base.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;


public class SimpleFileWriter extends AbstractFileWriter{

	public SimpleFileWriter() {
		super(new SimpleFileStrategy());
	}
	
	public String[] writeFile(File file, String filename, String... pathinfo){
		String homepath = strategy.getHomePath(pathinfo);
		String ext = filename.substring(filename.indexOf("."));
		String newfilename = strategy.getFileName() + ext;
		File target = strategy.getFile(getAbsoluteFilepath(homepath, newfilename));
		try {
			if(!StringUtil.isImageExtension(ext)){
				IOUtils.copyLarge(new FileInputStream(file), new FileOutputStream(target));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new String[]{homepath, newfilename};
	}
	
	private String getAbsoluteFilepath(String homepath, String filename){
		return Constants.ROOTPATH + homepath + filename;
	}
}
